﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class SEC_EditUserInfo
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iDataObject As UserType = Nothing
    Dim iFormActionOnData As FormActionOnDataEnum
#End Region

#Region "Overloads/Overrides"
    Public Overloads Sub Show(FormActionOnData As FormActionOnDataEnum, _
                              Optional User As UserType = Nothing)
        Me.Show()
        If User IsNot Nothing Then
            If User.UserType = SEC_UserTypesEnum.System And User.UserName.ToLower = "admin" Then
                AMsg.ShowDialog("Admin user is not allowed to be edited. " & _
                                "It is a built-in user created only for " & _
                                "system administration purpose.", MsgBoxStyle.Critical)
                Me.Close()
                Exit Sub
            Else
                Me.iDataObject = User
                Me.iFormActionOnData = FormActionOnDataEnum.UpdateRecord
            End If
        Else
            Me.iDataObject = New UserType(Guid.NewGuid)
            Me.iFormActionOnData = FormActionOnDataEnum.NewRecord
        End If
        Refresh_ListControls()
        Refresh_ControlsWithDataObject()
        Validate_UserNameAndDisplayIcon()
    End Sub

#End Region

#Region "Events"
    Private Sub SEC_EditUserInfo_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.InitializeThisFormAndControlSettings(False)
    End Sub
    Private Sub SEC_EditUserInfo_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Commit_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Commit_ImageButton.Click
        Me.Refresh_DataObjectWithControls()
        Try
            Dim secMan As New SecManager(AppSettings.LoggedUser)
            secMan.Update_UserInfo(Me.iDataObject)
            OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.UpdateRecord)
            Me.Close()
        Catch ex As Exception
            AMsg.ShowDialog(ex.Message, MsgBoxStyle.Critical, ex, Me.iDataObject)
        End Try
    End Sub
    Private Sub Cancel_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Cancel_ImageButton.Click
        Me.Close()
    End Sub
    Private Sub UserName_TextBox_GotFocus(sender As Object, e As System.EventArgs) Handles UserName_TextBox.GotFocus
        Validate_UserNameAndDisplayIcon()
    End Sub
    Private Sub UserName_TextBox_LostFocus(sender As Object, e As System.EventArgs) Handles UserName_TextBox.LostFocus
        Validate_UserNameAndDisplayIcon()
    End Sub
    Private Sub UserName_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles UserName_TextBox.TextChanged
        Me.Text = "Configure Login User " & Me.UserName_TextBox.Text
    End Sub
#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        If (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is UserType) Then
            If Me.iDataObject = DataObject Then
                Return Me
            End If
        End If
        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, _
                                               Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls
        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, RelatedControlDataCategoriesEnum.UserInformation, DataItem, ActionOnDataItem)
    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, _
                                                 DataTypeCategory As RelatedControlDataCategoriesEnum, _
                                                 Optional DataItem As Object = Nothing, _
                                                 Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        If DataTypeCategory = RelatedControlDataCategoriesEnum.UserInformation Then
            If ActionOnDataItem = FormActionOnDataEnum.UpdateRecord Then
                Me.Refresh_DepartmentsDBList()
            ElseIf ActionOnDataItem = FormActionOnDataEnum.DeleteRecord Then
                If DataItem IsNot Nothing Then
                    Dim user As UserType = DataItem
                    If user = Me.iDataObject Then
                        AMsg.ShowDialog("The data being edited has been deleted", MsgBoxStyle.Exclamation)
                        Me.Close()
                    End If
                Else
                    Me.Refresh_DepartmentsDBList()
                End If
            End If
        End If

    End Sub
    Private Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        With Me.iDataObject
            Me.UserId_TextBox.Text = .UserId.ToString
            Me.UserName_TextBox.Text = .UserName
            Me.Password_TextBox.Text = .PasswordVisible
            Me.Department_StylishComboBox.Text = .Department
            Me.AccessRole_StylishComboBox.SelectedItem = .AccessLevel
        End With
    End Sub
    Private Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls
        With Me.iDataObject
            .UserId = New Guid(Me.UserId_TextBox.Text)
            .UserName = Me.UserName_TextBox.Text
            .PasswordVisible = Me.Password_TextBox.Text
            .Department = Me.Department_StylishComboBox.Text
            .AccessLevel = Me.AccessRole_StylishComboBox.SelectedItem
        End With
    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_ListControls()
        Refresh_AccessRolesFixedList()
        Refresh_DepartmentsDBList()
    End Sub
    Private Sub Refresh_AccessRolesFixedList()

        Dim previousSelectedItem As SEC_AccessLevelsEnum = AccessRole_StylishComboBox.SelectedItem
        If AccessRole_StylishComboBox.SelectedItem IsNot Nothing Then
            previousSelectedItem = AccessRole_StylishComboBox.SelectedItem
        Else
            previousSelectedItem = SEC_AccessLevelsEnum.Reception
        End If
        AccessRole_StylishComboBox.Items.Clear()
        Dim selectedIndex As Integer = -1
        For Each iA As SEC_AccessLevelsEnum In [Enum].GetValues(GetType(SEC_AccessLevelsEnum))
            If (previousSelectedItem = iA) Then
                selectedIndex = AccessRole_StylishComboBox.Items.Add(iA)
            Else
                AccessRole_StylishComboBox.Items.Add(iA)
            End If
        Next
        If AccessRole_StylishComboBox.Items.Count > 0 Then
            If selectedIndex <> -1 Then
                AccessRole_StylishComboBox.SelectedIndex = selectedIndex
            Else
                AccessRole_StylishComboBox.SelectedItem = SEC_AccessLevelsEnum.Reception
            End If
        End If

    End Sub
    Private Sub Refresh_DepartmentsDBList()

        'fetch data
        Dim secMan As New SecManager(AppSettings.LoggedUser)
        Dim deps As ObjectModel.Collection(Of String) = secMan.Get_UserDepartments_All

        'get previous selection
        Dim prevSelection As String = Department_StylishComboBox.Text

        'clear combo
        Me.Department_StylishComboBox.Items.Clear()

        'Add new items
        Dim foundIndex As Integer = -1
        For Each s As String In deps
            If prevSelection = s Then
                foundIndex = Me.Department_StylishComboBox.Items.Add(s)
            Else
                Me.Department_StylishComboBox.Items.Add(s)
            End If
        Next
        If Me.Department_StylishComboBox.Items.Count > 0 Then
            If foundIndex <> -1 Then
                Department_StylishComboBox.SelectedIndex = foundIndex
            Else
                Department_StylishComboBox.SelectedIndex = 0
            End If
        End If

    End Sub
    Private Sub Validate_UserNameAndDisplayIcon()
        Dim res As String = ""

        If Me.UserName_TextBox.Text = "" Then
            ValidationStatusOk_PictureBox.Visible = False
            ValidationStatusFail_PictureBox.Visible = False
            Exit Sub
        Else
            Refresh_DataObjectWithControls()
            Dim secMan As New SecManager(AppSettings.LoggedUser)
            If secMan.Check_UserName_Exists(Me.iDataObject) Then
                ValidationStatusOk_PictureBox.Visible = False
                ValidationStatusFail_PictureBox.Visible = True
            Else
                ValidationStatusOk_PictureBox.Visible = True
                ValidationStatusFail_PictureBox.Visible = False
            End If
        End If

    End Sub
#End Region


End Class